package api

import (
	"gitee.com/DengAnbang/Computation/src/dbops"
	"gitee.com/DengAnbang/Computation/src/bean"
	"net/http"
	"gitee.com/DengAnbang/Computation/src/util"
	"fmt"
	"gitee.com/DengAnbang/Computation/src/dbops/excel"
)

/**
	* showdoc
	* @catalog socket文档/模板相关
	* @title 导入模板
	* @description 导入模板的接口
	* @method POST
	* @url /pc/template/importExcelTree
	* @param treeId  必选 string 树的ID
	* @param userId  必选 string 用户的ID
	* @return {"code":0,"type":0,"message":"","debug_message":"","data":"成功"}
	* @remark 最后修改时间:2019.01.11 10.30
	* @number 98
	*/
func ImportExcelTreeHttp(w http.ResponseWriter, r *http.Request) error {
	if r.Method == "GET" {
		fmt.Fprint(w, `<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <title>Upload</title>
</head>
<body>
<form method="POST" action="/pc/template/importExcelTree" enctype="multipart/form-data">
    Choose an image to upload: <input name="file" type="file" />
 	<input type="text" value="10013" name="userId">
 	<input type="text" value="asdasdasd" name="treeId">
    <input type="submit" value="上传" />
</form>
</body>
</html>`)
		return nil
	} else if r.Method == "POST" {
		treeId := util.GetValueFormRequest(r, "treeId")
		userId := util.GetValueFormRequest(r, "userId")
		if len(userId) <= 0 {
			return bean.NewErrorMessage("用户ID不能为空")
		}
		if len(treeId) <= 0 {
			return bean.NewErrorMessage("树的ID不能为空")
		}
		file, _, err := r.FormFile("file")
		if err != nil {
			return err
		}
		err = excel.ImportTree(file, treeId, userId)
		if err != nil {
			return err
		}
		return bean.NewSucceedMessage("成功")
	} else {
		return bean.NewErrorMessage("只支持POST的上传方式!")
	}
	return nil
}

/**
	* showdoc
	* @catalog socket文档/模板相关
	* @title 导出模板
	* @description 导出模板的接口
	* @method POST
	* @url /pc/template/exportExcelTree
	* @param treeId  必选 string 树的ID
	* @return 以文件流的形式返回,比如请求 /pc/template/exportExcelTree?treeId=asdasdasd,就直接返回一个文件
	* @remark 最后修改时间:2019.01.11 10.30
	* @number 99
	*/
func ExportExcelTreeHttp(w http.ResponseWriter, r *http.Request) error {
	treeId := util.GetValueFormRequest(r, "treeId")
	outFile, treeName, err := excel.ExportTree(treeId)
	if err != nil {
		return err
	}
	w.Header().Set("Content-Disposition", "attachment; filename="+treeName+".xlsx")
	outFile.Write(w)
	return nil
}
func AddTemplateNodeHttp(_ http.ResponseWriter, r *http.Request) error {
	treeNodeId := util.GetValueFormRequest(r, "treeNodeId")
	no := util.GetValueFormRequest(r, "no")
	name := util.GetValueFormRequest(r, "name")
	unit := util.GetValueFormRequest(r, "unit")
	designNum := util.GetValueFormRequest(r, "designNum")
	price := util.GetValueFormRequest(r, "price")
	remarks := util.GetValueFormRequest(r, "remarks")
	_, err := dbops.AddTemplateNode(treeNodeId, no, name, unit, designNum, price, remarks)
	if err != nil {
		return err
	}
	return bean.NewSucceedMessage("添加成功")
}

func AmendTemplateNodeHttp(_ http.ResponseWriter, r *http.Request) error {
	templateId := util.GetValueFormRequest(r, "templateId")
	treeNodeId := util.GetValueFormRequest(r, "treeNodeId")
	no := util.GetValueFormRequest(r, "no")
	name := util.GetValueFormRequest(r, "name")
	unit := util.GetValueFormRequest(r, "unit")
	designNum := util.GetValueFormRequest(r, "designNum")
	price := util.GetValueFormRequest(r, "price")
	remarks := util.GetValueFormRequest(r, "remarks")
	err := dbops.AmendTemplateNode(templateId, treeNodeId, no, name, unit, designNum, price, remarks)
	if err != nil {
		return err
	}
	return bean.NewSucceedMessage("修改成功")
}

func SelectTemplateListHttp(_ http.ResponseWriter, r *http.Request) error {
	treeNodeId := util.GetValueFormRequest(r, "treeNodeId")
	beans, err := dbops.SelectTemplateList(treeNodeId)
	if err != nil {
		return err
	}
	return bean.NewSucceedMessage(beans)
}

func DeleteTemplateByIDHttp(_ http.ResponseWriter, r *http.Request) error {
	templateId := util.GetValueFormRequest(r, "templateId")
	err := dbops.DeleteTemplateByID(templateId)
	if err != nil {
		return err
	}
	return bean.NewSucceedMessage("删除成功")
}

func DeleteTemplateByNodeHttp(_ http.ResponseWriter, r *http.Request) error {
	treeNodeId := util.GetValueFormRequest(r, "treeNodeId")
	err := dbops.DeleteTemplateByNode(treeNodeId)
	if err != nil {
		return err
	}
	return bean.NewSucceedMessage("删除成功")
}
